package com.neouton.crm.sales.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neouton.common.util.Constants;
import com.neouton.crm.op.mapper.SalesOpportunityMapper;
import com.neouton.crm.sales.entity.SalesStageVO;
import com.neouton.crm.sales.mapper.SalesStageMapper;
import com.neouton.crm.sales.service.SalesStageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SalesStageServiceImpl extends ServiceImpl<SalesStageMapper, SalesStageVO> implements SalesStageService {
    @Resource
    private SalesStageMapper salesStageMapper;
    @Resource
    private SalesOpportunityMapper opportunityMapper;

    @Override
    @Transactional
    public boolean changeStage(SalesStageVO salesStageVO) {
        // 插入行表
        salesStageMapper.insert(salesStageVO);
        // 获取行表最新记录
        List<SalesStageVO> salesStages = salesStageMapper.queryStageInfo(salesStageVO.getSalesRelationId(), Constants.OPPORTUNITY_MODEL_ID);
        SalesStageVO newestStage = salesStages.get(0);

        // 更新主表信息
        opportunityMapper.opportunityUpdateStage(newestStage);
        return true;
    }

    @Override
    public List<SalesStageVO> queryStageInfo(Integer salesRelationId, Integer stageClass) {
        return salesStageMapper.queryStageInfo(salesRelationId, stageClass);
    }

    @Override
    public int insert(SalesStageVO salesStageVO) {
        return salesStageMapper.insert(salesStageVO);
    }

}
